package com.jiankang.askhealth.core.user.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;

import com.jiankang.askhealth.aware.UserAware;
import com.jiankang.askhealth.base.web.PagingAction;
import com.jiankang.askhealth.constant.SessionName;
import com.jiankang.askhealth.core.api.base.ApiErrorBean;
import com.jiankang.askhealth.core.api.base.ApiResult;
import com.jiankang.askhealth.core.api.bean.ApiUserBean;
import com.jiankang.askhealth.core.user.bean.LoginResult;
import com.jiankang.askhealth.core.user.bean.UserBean;
import com.jiankang.askhealth.util.ApiErrorCode;
import com.jiankang.askhealth.util.ApiThreadLocal;
import com.jiankang.askhealth.util.StringUtils;


public class UserAction extends PagingAction implements ServletRequestAware, ServletResponseAware, UserAware {

	private UserBean loginUser;

	private HttpServletRequest request;
	private HttpServletResponse response;
	
	@Override
	public void setLoginUser(UserBean userBean) {
		this.loginUser = userBean;
	}

	public String login() {
		if (request.getParameter("submit") != null) {
			if (request.getSession().getAttribute(SessionName.LOGIN_USER) != null) {
				return SUCCESS;
			}
			String loginname = request.getParameter("loginname");
			String loginpassword = request.getParameter("loginpassword");
			if (StringUtils.isNotEmpty(loginname)
					&& StringUtils.isNotEmpty(loginpassword)) {
				//LoginResult loginresult = userService.getUserByLogin(loginname, loginpassword);
				
				UserBean userBean = new UserBean();
	            userBean.setAdmin(true);
	            userBean.setUsername("杨伟-w");
	            userBean.setRole("a");
	            userBean.setUserId(1L);
	            
	            LoginResult loginresult = new LoginResult();
	            loginresult.setUserBean(userBean);
	            loginresult.setExist(true);
	            
				if (!loginresult.isExist()) {
					return INPUT;
				} else {
					request.getSession().setAttribute(SessionName.LOGIN_USER,
							loginresult.getUserBean());
					return SUCCESS;
				}
			}
		}

		return INPUT;
	}
	
	public String logout() {
		if (request.getSession().getAttribute(SessionName.LOGIN_USER) != null) {
			request.getSession().removeAttribute(SessionName.LOGIN_USER);
			request.getSession().invalidate();
			return SUCCESS;
		}
		return SUCCESS;
	}

	public String apiLogin() {
		ApiUserBean userBean = new ApiUserBean();
		String loginname = request.getParameter("loginname");
		String loginpassword = request.getParameter("loginpassword");
		if(StringUtils.isEmpty(loginname)){
            ApiThreadLocal.setResultData(new ApiErrorBean(ApiErrorCode.LOGIN_NAME_IS_NULL));
            return null;
        }
        else if(StringUtils.isEmpty(loginpassword)){
        	ApiThreadLocal.setResultData(new ApiErrorBean(ApiErrorCode.LOGIN_PASSWORD_IS_NULL));
            return null;
        }
		if (StringUtils.isNotEmpty(loginname)
				&& StringUtils.isNotEmpty(loginpassword)) {
			//LoginResult loginresult = userService.getUserByLogin(loginname, loginpassword);
			
	        userBean.setUsername("杨伟-w");
	        userBean.setRole("a");
	        userBean.setUserId(1L);
	        
		}
        userBean.setResult(new ApiResult(true));
        ApiThreadLocal.setResultData(userBean);
        return null;
	}
	
	
	@Override
	public void setServletResponse(HttpServletResponse response) {
		this.response = response;
	}

	@Override
	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
	}

}
